<template>
	<view class="doctorSelectContainers">
		<view class="doctorSelectContainers_title">
			{{title}}
		</view>
		<view class="doctorSelectContainers_content">
			<view class="doctorSelectContainers_item" v-for="(doctor, index) in doctorList" :key="index">
				<view class="doctorSelectContainers_item_left" v-if="type=='sq'">
					<u-checkbox-group @change="getcheck(doctor, index)">
						<u-checkbox class="cardItem_box" size="28rpx" iconSize="24rpx"></u-checkbox>
					</u-checkbox-group>
				</view>
				<view class="doctorSelectContainers_item_left" v-if="type=='zs'">
					<view class="doctorSelectContainers_item_left_btn" @tap="handleEdit(doctor)">
						选择
					</view>
				</view>
				<view class="doctorSelectContainers_item_left" v-if="type=='fp'">
					<view class="doctorSelectContainers_item_left_btn" @tap="handleScheduling(doctor)">
						分配
					</view>
				</view>
				<view class="doctorSelectContainers_item_right">
					<view class="doctorSelectContainers_item_right_item">
						<view class="doctorSelectContainers_item_right_item_label">
							姓名：
						</view>
						<view class="doctorSelectContainers_item_right_item_infos">
							{{doctor.name}}
						</view>
					</view>
					<view class="doctorSelectContainers_item_right_item">
						<view class="doctorSelectContainers_item_right_item_label">
							所属机构：
						</view>
						<view class="doctorSelectContainers_item_right_item_infos">
							{{doctor.institutionName}}
						</view>
					</view>
					<view class="doctorSelectContainers_item_right_item" v-if="doctor.deptName">
						<view class="doctorSelectContainers_item_right_item_label">
							科室：
						</view>
						<view class="doctorSelectContainers_item_right_item_infos">
							{{doctor.deptName}}
						</view>
					</view>
					<view class="doctorSelectContainers_item_right_item" v-if="doctor.medicalTitleName">
						<view class="doctorSelectContainers_item_right_item_label">
							职称：
						</view>
						<view class="doctorSelectContainers_item_right_item_infos">
							{{doctor.medicalTitleName}}
						</view>
					</view>

				</view>
			</view>
		</view>
		<view class="doctorSelectContainers_btnGroup">
			<u-button type="info" size="mini" text="关 闭" style="width:100rpx;margin:0rpx 24rpx;"
				@tap="closed()"></u-button>
			<u-button type="primary" v-if="type=='sq'" size="mini" text="确 定"
				:disabled="(expertIdArr&&expertIdArr.length>0)?false:true" style="width:100rpx;margin:0rpx 24rpx;"
				@tap="comfirm()"></u-button>
		</view>

	</view>
</template>

<script>
	import moment from 'moment'
	import request from '@/utils/request'
	import Qs from 'qs'
	export default {
		name: "doctorSelect",
		props: ['type', 'orders', 'title'],
		data() {
			return {
				doctorList: [],
				expertIdArr: []
			}
		},
		created() {


		},
		mounted() {
			if (this.type == 'sq') {
				this.inviteTableDataRequest()
			} else if (this.type == 'zs') {
				this.getFinalDoctorList()
			} else if (this.type == 'fp') {
				console.log('this.orders', this.orders)
				this.doctorsTableDataRequest(this.orders.id, this.orders.serviceCenterId, this.orders.patients[0].id)
			}

		},
		methods: {
			closed() {
				this.$emit('closed')
			},
			getcheck(item, index) {
				if (item.checked) {
					item.checked = false
				} else {
					item.checked = true
				}
				this.$set(this.doctorList, index, item)
				this.getcheckArr()
			},
			getcheckArr() {
				this.expertIdArr = []
				const expertIdArr = []
				this.doctorList.forEach((t, index) => {
					if (t.checked) {
						expertIdArr.push(t.id)
					}

				})
				console.log(expertIdArr)
				this.expertIdArr = expertIdArr
			},
			comfirm() {
				if (this.type == 'sq') { //确认选中的邀请医生
					this.inviteSpecialist()
				} else { //终审专家选定

				}
			},
			doctorsTableDataRequest(orderId, serviceCenterId, patientsId) {
				try {
					request({
						method: 'GET',
						url: `/consultations/${serviceCenterId}/${patientsId}/distributionList`,
						params: {
							page: 0,
							size: 999
						}
					}).then(res => {
						if (res && res.code === 200) {
							// 数据处理
							this.doctorList = []
							if (res.data) {
								res.data.forEach((t, index) => {
									const user = {
										index,
										id: t.id,
										name: t.name,
										institutionName: t.institutionName,
										deptName: t.deptName,
										medicalTitleName: t.medicalTitleName,
										createTime: moment(t.createTime).format('YYYY-MM-DD'),
										statusName: t.status ? '冻结' : '正常',
										phone: t.phone,
										checked: false,
										orderId: orderId,
										patientsId: patientsId
									}
									this.doctorList.push(user)
								})
							}
						}
					})
				} catch (e) {
					this.$u.toast('请求失败')
				}
			},
			inviteTableDataRequest() { // 邀请专家
				try {
					request({
						method: 'GET',
						url: `/consultations/${this.orders.serviceCenterId}/${this.orders.patients[0].id}/invitation-experts`,
						params: {
							page: 0,
							size: 999
						}
					}).then(res => {
						if (res && res.code === 200) {
							// 数据处理
							this.doctorList = []
							if (res.data) {
								res.data.forEach((t, index) => {
									const user = {
										index,
										id: t.id,
										name: t.name,
										institutionName: t.institutionName,
										deptName: t.deptName,
										medicalTitleName: t.medicalTitleName,
										createTime: moment(t.createTime).format('YYYY-MM-DD'),
										statusName: t.status ? '冻结' : '正常',
										phone: t.phone,
										checked: false,
									}
									this.doctorList.push(user)
								})
							}
						}
					})
				} catch (e) {
					this.$u.toast('请求失败')
				}
			},
			getFinalDoctorList() { //终审专家
				request({
					method: 'GET',
					url: `/institutions/getFinalDoctors`
				}).then(res => {
					if (res && res.code === 200) {
						// 数据处理
						this.doctorList = []
						if (res.data) {
							res.data.forEach((t, index) => {
								const user = {
									index,
									id: t.id,
									name: t.name,
									institutionName: t.institutionName,
									deptName: t.deptName,
									checked: false,
									medicalTitleName: t.medicalTitleName,
									createTime: moment(t.createTime).format('YYYY-MM-DD'),
									statusName: t.status ? '冻结' : '正常',
									phone: t.phone,
								}
								this.doctorList.push(user)
							})
						}
					}
				})
			},
			//  确认邀请
			async inviteSpecialist() {
				const expertIdArr = this.expertIdArr
				let params = {
					expertIdArr
				}
				let dataparasms = Qs.stringify(params, {
					arrayFormat: 'repeat'
				})
				const res = await request({
					method: 'PUT',
					url: `/consultations/${this.orders.patients[0].id}/invite?${dataparasms}`,
				})

				if (res && res.code === 200) { //刷新页面
					this.$u.toast('邀请成功')
					this.$emit('doctorComfirm')
				} else {
					this.$u.toast('邀请失败')
				}
			},
			// 确认终审医生
			handleEdit(row) {
				let that = this
				uni.showModal({
					title: '提示',
					content: `确认选择${row.name}为终审医生?`,
					success: function(res) {
						if (res.confirm) {
							that.$emit('finalDoctorcomfirn', row.id)
							// 用户点击了确定按钮的相关逻辑可以放在这里
						} else if (res.cancel) {
							console.log('用户点击取消');
							// 用户点击了取消按钮的相关逻辑可以放在这里
						}
					}
				});
			},
			// 确认分配医生
			handleScheduling(currentRowDocTor) {
				let that = this
				uni.showModal({
					title: '提示',
					content: `分配给${currentRowDocTor.name}?`,
					success: async (res) => {
						if (res.confirm) {
							const result = await request({
								method: 'GET',
								url: `/orders/${currentRowDocTor.orderId}/${currentRowDocTor.patientsId}/allocate/${currentRowDocTor.id}`
							})
							if (result && result.code === 200) { //刷新页面
								that.$u.toast('分配成功')
								that.$emit('schedulingDoctorComfirm')
							} else {
								that.$u.toast('分配失败')
							}

							// 用户点击了确定按钮的相关逻辑可以放在这里
						} else if (res.cancel) {
							console.log('用户点击取消');
							// 用户点击了取消按钮的相关逻辑可以放在这里
						}
					}
				});
			},

		}
	}
</script>

<style lang="scss" scoped>
	.doctorSelectContainers {
		width: 100%;
		height: 100%;

		&_title {
			text-align: center;
			font-size: 30rpx;
			font-weight: 700;
			line-height: 50rpx;
		}

		&_content {
			height: calc(100% - 110rpx);
			padding: 0 24rpx;
			overflow-y: auto;
		}

		&_btnGroup {
			height: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		&_item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 2rpx solid #ddd;
			padding: 16rpx 0;

			&_left {
				width: 70rpx;

				&_btn {
					color: rgb(55, 112, 246);
					font-size: 26rpx;
				}
			}

			&_right {
				width: calc(100% - 70rpx);
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				font-size: 26rpx;
				line-height: 40rpx;

				&_item:last-child {
					margin-right: 0rpx;
				}

				&_item {
					display: flex;
					align-items: center;
					margin-right: 12rpx;

					&_label {
						color: #999;

					}

					&_infos {
						color: #333;
					}
				}
			}
		}
	}
</style>